package com.gerzel.stop.server;

import com.gerzel.stop.client.StopHabitsService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import java.util.Date;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import java.util.List;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import com.gerzel.stop.server.UserHabits;
import com.gerzel.stop.server.PMF;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings({ "unchecked", "serial" })
public class StopHabitsServiceImpl extends RemoteServiceServlet implements
		StopHabitsService {
	
		public Date stopDrinkServer() {		
			UserService userService = UserServiceFactory.getUserService();
			User user = userService.getCurrentUser();
			if (user != null) {		
			PersistenceManager pm = PMF.get().getPersistenceManager();
			Query query = pm.newQuery(UserHabits.class, "UserName == UserNameParam");
            query.declareParameters("com.google.appengine.api.users.User UserNameParam");
            try {
		        List<UserHabits> result = (List<UserHabits>) query.execute(user);
		        if (result!=null && !result.isEmpty()) {		        	
		        	return result.get(0).getDrinkDate();
		        } else {
		        	return null;
		        }
		    } finally {
		        query.closeAll();
		    }
		} else {
		return null;
		}
	}

	public Date stopSmokeServer() {
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		if (user != null) {		
			PersistenceManager pm = PMF.get().getPersistenceManager();
			Query query = pm.newQuery(UserHabits.class, "UserName == UserNameParam");
            query.declareParameters("com.google.appengine.api.users.User UserNameParam");
            try {
		        List<UserHabits> result = (List<UserHabits>) query.execute(user);
		        if (result!=null && !result.isEmpty()) {		        	
		        	return result.get(0).getSmokeDate();
		        } else {
		        	return null;
		        }
		    } finally {
		        query.closeAll();
		    }
		} else {
		return null;
		}
	}

	public String habitServer(Date drinkDate, Date smokeDate) {
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		if (user != null) {
			PersistenceManager pm = PMF.get().getPersistenceManager();
			Query query = pm.newQuery(UserHabits.class, "UserName == UserNameParam");
            query.declareParameters("com.google.appengine.api.users.User UserNameParam");
            try {
            	List<UserHabits> result = (List<UserHabits>) query.execute(user);
		        if (result!=null && !result.isEmpty()) {		        	
		        	try {
		        		result.get(0).setDrinkDate(drinkDate);
		        		result.get(0).setSmokeDate(smokeDate);		        		
	            		} finally {
	            			pm.close();
	            			};
		        } else {
		        	UserHabits habits = new UserHabits(user, drinkDate, smokeDate);
	            	try {
	            		pm.makePersistent(habits);
	            		} finally {
	            			pm.close();
	            			};
		        }
		    } finally {
		        query.closeAll();
		    }
		}
		return null;
	}

	public String userServer() {
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		if (user!=null) {
			return user.getNickname() + " <a href=\"" + userService.createLogoutURL("http://stop.gerzel.com") + "\">Выход</a>";
		} else {
			return " <a href=\"" + userService.createLoginURL("http://stop.gerzel.com") + "\">Вход</a>";
		}
	}
}